package cate.game.admin.menu;

import cate.game.GameBody;
import cate.game.admin.AdminMenu;
import cate.game.admin.cross.CrossAdminCMDs;
import cate.game.admin.cross.CrossAdminReq;
import cate.game.event.EventPublisher;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;

public class MenuSetup implements EventPublisher {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    protected String type;

    public MenuSetup(String type) {
        this.type = type;
    }

    protected void reg(GameBody gameBody, String name, int paramNum, String description, AdminMenuRunnable runable) {
        new AdminMenu(gameBody, name, paramNum, description, this.type).setRunnable(runable);
    }

    protected String toString(List<String> params, int index, String defaultValue) {
        if (params.size() > index) {
            String str = params.get(index);
            if (StringUtils.isNotBlank(str)) {
                return str;
            }
        }
        return defaultValue;
    }

    protected int toInt(List<String> params, int index, int defaultValue) {
        if (params.size() > index) {
            String str = params.get(index);
            if (StringUtils.isNotBlank(str)) {
                return Integer.parseInt(str);
            }
        }
        return defaultValue;
    }

    protected long toLong(List<String> params, int index, long defaultValue) {
        if (params.size() > index) {
            String str = params.get(index);
            if (StringUtils.isNotBlank(str)) {
                return Long.parseLong(str);
            }
        }
        return defaultValue;
    }

    protected double toDouble(List<String> params, int index, double defaultValue) {
        if (params.size() > index) {
            String str = params.get(index);
            if (StringUtils.isNotBlank(str)) {
                return Double.parseDouble(str);
            }
        }
        return defaultValue;
    }

    protected byte toByte(List<String> params, int index, byte defaultValue) {
        if (params.size() > index) {
            String str = params.get(index);
            if (StringUtils.isNotBlank(str)) {
                return Byte.parseByte(str);
            }
        }
        return defaultValue;
    }

    protected boolean toBoolean(List<String> params, int index, boolean defaultValue) {
        if (params.size() > index) {
            String str = params.get(index);
            if (StringUtils.isNotBlank(str)) {
                if (str.equals("1") || str.equals("true")) {
                    return true;
                }
            }
        }
        return defaultValue;
    }

    /**
     * 调用跨服GM命令
     */
    protected final String callCrossGM(GameBody game, String cmd, String roleUid, Object... args) {
        return Optional.ofNullable(game.rpcSend().requestResponseCross(
                        CrossAdminCMDs.CROSS_MSG,
                        new CrossAdminReq(cmd, game.config.srvId, roleUid, args),
                        String.class
                ))
                .orElse("failed");
    }
}
